<?php
namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Exception;

class Address extends Api{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**我的收货地址列表**/
    public function addresslist(){
        $userinfo = $this->auth->getUserinfo();
        $addressModel = new \app\admin\model\address\Address();
        $addresslist = collection($addressModel->where(['user_id'=>$userinfo['id']])->order('sort desc,id desc')->select());
        if($addresslist){
            $addresslist = $addresslist->toArray();
            $this->success('成功',$addresslist);
        }else{
            $this->success('成功',[]);
        }
    }

    /**设置默认**/
    public function setdefaultaddress(){
        $id = $this->request->param('id');
        $userinfo = $this->auth->getUserinfo();
        $addressModel = new \app\admin\model\address\Address();
        $addressinfo = $addressModel->where(['id'=>$id])->find();
        if($addressinfo){
            $addressinfo = $addressinfo->toArray();
            if($addressinfo['user_id'] != $userinfo['id']){
                $this->error('您不能修改别人的地址');
            }
            Db::startTrans();
            try{
                $addressModel->where(['id'=>$id])->update(['is_default'=>1]);
                $where['id']=['<>',$id];
                $addressModel->where($where)->update(['is_default'=>0]);
                Db::commit();
                $this->success('设置成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }else{
            $this->error('ID不存在');
        }

    }

    /**收货地址详情**/
    public function addressdetail(){
        $id = $this->request->param('id');
        $addressModel = new \app\admin\model\address\Address();
        $addressinfo = collection($addressModel->where(['id'=>$id])->find());
        if($addressinfo){
            $addressinfo = $addressinfo->toArray();
            $this->success('成功',$addressinfo);
        }else{
            $this->error('ID不存在');
        }
    }

    /**设置收货地址**/
    public function setaddress(){
        $id = $this->request->param('id');
        $username =$this->request->param('username');
        $mobile =$this->request->param('mobile');
        $province =$this->request->param('province');
        $city =$this->request->param('city');
        $district =$this->request->param('district');
        $address =$this->request->param('address');

        $userinfo = $this->auth->getUserinfo();
        $addressModel = new \app\admin\model\address\Address();
        if($id==''){
            $indata['user_id'] = $userinfo['id'];
            $indata['username'] = $username;
            $indata['mobile'] = $mobile;
            $indata['province'] = $province;
            $indata['city'] = $city;
            $indata['district'] = $district;
            $indata['address'] = $address;
            $indata['create_time'] = time();
            $indata['update_time'] = time();
            $addressModel->insert($indata);
        }else{
            $addressModel->where(['id'=>$id])->update([
                'username'=>$username,
                'mobile'=>$mobile,
                'province'=>$province,
                'city'=>$city,
                'district'=>$district,
                'address'=>$address,
                'update_time'=>time()
            ]);
        }
        $this->success('设置成功');
    }

    /**置顶**/
    public function totop(){
        $id = $this->request->param('id');
        $addressModel = new \app\admin\model\address\Address();
        // 获取当前最大的sort值
        $maxSort = $addressModel->max('sort');

        // 将当前记录的sort设置为最大值+1
        $addressModel->where(['id'=>$id])->update([
            'sort' => $maxSort + 1
        ]);
        $this->success('置顶成功');
    }

    /**删除**/
    public function deleteaddress(){
        $id = $this->request->param('id');
        $userinfo = $this->auth->getUserinfo();
        $addressModel = new \app\admin\model\address\Address();
        $addressinfo = $addressModel->where(['id'=>$id])->find()->toArray();
        if($addressinfo['user_id']!=$userinfo['id']){
            $this->error('您不能删除别人的地址');
        }
        $addressModel->where(['id'=>$id])->delete();
        $this->success('删除成功');
    }
}